package com.qf.filters;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.http.protocol.RequestContent;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@Component
public class MyPostFilter02 extends ZuulFilter {

//    过滤器类型
    @Override
    public String filterType() {
//        前置
        return FilterConstants.POST_TYPE;
    }
//      过滤器顺序
    @Override
    public int filterOrder() {
        return 2;
    }

// 是否执行当前方法
    @Override
    public boolean shouldFilter() {
        return true;
    }

//    要执行的方法
    @Override
    public Object run() throws ZuulException {
        System.out.println("MyPostFilter02 执行了");

//        RequestContext context = RequestContext.getCurrentContext();
//
//        HttpServletResponse response = context.getResponse();
//
//////        下游服务返回的数据   在后置里面   IO流只能 读取处理一次  无法返回到客户端    必须手动单独返回结果
//        InputStream inputStream = context.getResponseDataStream();
//        BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
//
//        String s=null;
//        StringBuffer stringBuffer=new StringBuffer();
//
////
//        try {
//            while ((s=reader.readLine())!=null){
//                stringBuffer.append(s);
//            }
//            System.out.println("返回的结果"+stringBuffer.toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        response.setHeader("content-Type","text/html;charset=utf-8");
//
//
//        context.setResponseBody(stringBuffer.toString());


        return null;
    }
}
